package com.pzhu.controller.wx;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pzhu.model.entity.Address;
import com.pzhu.model.help.ResponseResult;
import com.pzhu.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

/**
 * @Author : zqh
 * @Date : 2023/12/3 0003 下午 2:38
 * @Version: 1.0
 */
@Slf4j
@Api(tags = "小程序用户地址相关的接口")
@RestController
@RequestMapping("/wx/address")
public class WxAddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation("获取个人地址信息")
    @GetMapping("/getByUserId/{id}")
    @ApiImplicitParam(name = "id",value = "用户ID",required = true)
    public ResponseResult getByUserId( @PathVariable(value = "id") Long id) {
       List<Address> list =  addressService.list(new LambdaQueryWrapper<Address>().eq(Address::getUserId, id));
        Map<String, Object> map = new HashMap<>();
        map.put("list",list);
       return ResponseResult.ok(map);
    }

    @ApiOperation("添加或修改地址信息")
    @PostMapping("/save")
    public ResponseResult save(@RequestBody @Valid Address address) {
        if (Objects.isNull(address) || address.getId() == -1) {
            address.setCreateTime(new Date());
            addressService.save(address);
        }else {
            address.setUpdateTime(new Date());
            addressService.updateById(address);
        }
        return ResponseResult.ok();
    }

    @ApiOperation("根据Id查询")
    @GetMapping("/getById/{id}")
    @ApiImplicitParam(name = "id",value = "地址ID",required = true)
    public ResponseResult getById( @PathVariable(value = "id") Long id) {
        Address address = addressService.getById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("address", address);
        return ResponseResult.ok(map);
    }

    @ApiOperation("根据ID删除")
    @PutMapping("/delById/{id}")
    @ApiImplicitParam(name = "id",value = "地址ID",required = true)
    public ResponseResult delById( @PathVariable(value = "id") Long id) {
        addressService.removeById(id);
        return ResponseResult.ok();
    }
}
